﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VO;

namespace DAL
{
    public class LogAcessoDAL
    {
        #region Insert
        public void Insert(LogAceesso obj)
        {
            var contexto = new CallcenterEntities();
            var objC = new tbCCLogAcesso();
            objC = PopulaObjeto(obj, objC);
            contexto.AddTotbCCLogAcesso(objC);
            contexto.SaveChanges();
        }
        #endregion

        #region Select
        public IList<LogAceesso> Select(int idUsuario = 0)
        {
            var contexto = new CallcenterEntities();

            IQueryable<tbCCLogAcesso> logAceessos;

            logAceessos = (from tb in contexto.tbCCLogAcesso
                         where (tb.idUsuario== idUsuario || idUsuario == 0)
                         select tb);

            //Carrega lista
            var list = PopulaObjeto(logAceessos);

            return list;
        }
        #endregion

        #region SelectAll
        public IList<LogAceesso> SelectAll(int idUsuario = 0)
        {
            var contexto = new CallcenterEntities();

            IQueryable<tbCCLogAcesso> logAcessos;

            logAcessos = (from tb in contexto.tbCCLogAcesso
                          where (tb.idUsuario == idUsuario || idUsuario == 0)
                         select tb);

            //Carrega lista
            var list = PopulaObjeto(logAcessos);

            return list;
        }
        #endregion

        #region PopulaObjeto
        protected static tbCCLogAcesso PopulaObjeto(LogAceesso obj, tbCCLogAcesso tabela)
        {
            tabela.nomePagina = obj.nomePagina;
            tabela.idUsuario = obj.idUsuario;
            return tabela;
        }

        protected static IList<LogAceesso> PopulaObjeto(IQueryable<tbCCLogAcesso> reader)
        {
            IList<LogAceesso> list = new List<LogAceesso>();
            foreach (var obj in reader)
            {
                var newObj = new LogAceesso();
                newObj.nomePagina= obj.nomePagina;
                newObj.idUsuario = (int) obj.idUsuario;
                list.Add(newObj);
            }
            return list;
        }
        #endregion
    }
}
